//
//  ViewController.swift
//  MultiTouch
//
//  Created by 移云 on 2020/5/12.
//  Copyright © 2020 MaoYeYeDeJiangCuCha. All rights reserved.
//

import UIKit
//多点触控
class ViewController: UIViewController {

    let imageArr:[String] = ["spark_blue","spark_red"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.view.backgroundColor = .black
        self.view.isMultipleTouchEnabled = true //开启多点触控
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch:AnyObject in touches {
            let p = touch.location(in: self.view)
            let lightImageView = UIImageView.init(image: UIImage(named: "spark_cyan"))
            self.view.addSubview(lightImageView)
            lightImageView.center = p
            
            UIView.animate(withDuration: 2, animations: {
                lightImageView.alpha = 0
            }) { (true) in
                lightImageView.removeFromSuperview()
            }
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        var i = 0
        for touch:AnyObject in touches {
            let p = touch.location(in: self.view)
            let lightImageView = UIImageView.init(image: UIImage(named: imageArr[i]))
            self.view.addSubview(lightImageView)
            lightImageView.center = p
            
            UIView.animate(withDuration: 2, animations: {
                lightImageView.alpha = 0
            }) { (true) in
                lightImageView.removeFromSuperview()
            }
            i += 1
        }
    }


}

